﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ReprojectWebMercator
{
    public class vec2
    {
        public float x, y;

        public override string ToString()
        {
            return x + "," + y;
        }

        #region 属性
        public vec4 xyxy
        {
            get { return new vec4(x, y,x,y); }
        }
        public vec3 get3(string v)
        {
            return new vec3(getT(v[0]), getT(v[1]), getT(v[2]));
        }
        private float getT(char t)
        {
            if (t == 'x') return x;
            if (t == 'y') return y;
            return 0;
        }

        #endregion

        #region 构造函数
        public vec2() { }
        public vec2(float x, float y)
        {
            this.x = x;
            this.y = y;
        }
        public vec2(double x, double webMercatorT)
        {
            this.x = (float)x;
            this.y = (float)webMercatorT;
        }
        public vec2(string json)
        {
            var jvalue = JsonConvert.DeserializeObject<vec2>(json);
            x = jvalue.x;
            y = jvalue.y;
        }
        #endregion

        public static vec2 operator -(vec2 v1, vec2 v2)
        {
            return new vec2(v1.x - v2.x, v1.y - v2.y);
        }
        public static vec2 operator -(double v1, vec2 v2)
        {
            return new vec2(v1 - v2.x, v1 - v2.y);
        }
        public static vec2 operator +(vec2 v1, vec2 v2)
        {
            return new vec2(v1.x + v2.x, v1.y + v2.y);
        }
        public static vec2 operator +(vec2 v1, float v2)
        {
            return new vec2(v1.x + v2, v1.y + v2);
        }
        public static vec2 operator *(float t, vec2 v1)
        {
            return new vec2(v1.x * t, v1.y * t);
        }
        #region 乘以
        public static vec2 operator *(vec2 v1, double t)
        {
            return new vec2(v1.x * t, v1.y * t);
        }
        public static vec2 operator *(vec2 v1, float t)
        {
            return new vec2(v1.x * t, v1.y * t);
        }
        public static vec2 operator *(vec2 v1, vec2 t)
        {
            return new vec2(v1.x * t.x, v1.y * t.y);
        }
        public static vec2 operator *(vec2 v1, mat2 t)
        {
            var x = v1.x * t.m11 + v1.y * t.m21;
            var y = v1.x * t.m12 + v1.y * t.m22;
            return new vec2(x, y);
        }
        #endregion
        public static vec2 operator /(vec2 v1,float t)
        {
            return new vec2(v1.x / t, v1.y / t);
        }
        public static vec2 operator /(vec2 v1, vec2 t)
        {
            return new vec2(v1.x / t.x, v1.y / t.y);
        }
    }
}
